import axios from "axios";
import { ElMessage } from "element-plus";
import router from '@/router'
import { useUserStore } from "@/stores/user";
//基础地址
const baseURL = '/api'

//axios
const instance = axios.create({
    baseURL: baseURL,
    //请求超时时间
    timeout:5000,
    withCredentials:true
    // headers:{
    //     'Access-Control-Allow-Origin':'*'
    // }
})

//响应状态码
const RESP_CODE = {
    success:200,
    server_error:500,
    param_error:400
}

//请求拦截器
instance.interceptors.request.use(
    (config)=>{
        const userStore = useUserStore()
        const token = {userStore}
        //console.log(token)
         if(token){
             config.headers.Authorization = `Bearer${token}`
         }
        return config
    },
    (error)=>{
        return Promise.reject(error)
    }
)
//响应拦截器
instance.interceptors.response.use(
    (response)=>{
        //对响应成功进行公共处理
        return response
    },
    (error)=>{
        return Promise.reject(error);
    }
)
//导出实例
export default instance